﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
namespace MsSpatialTools
{
    public class RunMsscmd
    {
        private static string shapePath = "";
        private static string importTbName = "";
        public static string getMsscmd
        {
            get 
            {
                
                string msscmd = Application.StartupPath + "\\msscmd.exe";
                return msscmd;
            }
        }
        public static string getDeployPars
        {
            get
            {
                string deployPars;
                deployPars = "-deploy -server={0} -db={1}";
                deployPars = string.Format(deployPars, DBHelper.SqlServer, DBHelper.Database);
                if (!DBHelper.Trusted_Connection)
                {
                    deployPars += "-deploy -login={0} -password={1}";
                    deployPars = string.Format(deployPars, DBHelper.UserID, DBHelper.Password);
                }
                return deployPars;
            }
        }
        public static string getImportShapePars
        {
            get
            {
                string importShapePars = getDeployPars.Replace("-deploy", "");
                importShapePars += " -table=" + importTbName + " -import=shp ";
                importShapePars += " -shp_filename=\"" + shapePath + "\"";
                return importShapePars;
            }
        }
        private static string Execute(string pars)
        {
            try
            {
                Process myprocess = new Process();
                myprocess.StartInfo.UseShellExecute = false;
                myprocess.StartInfo.RedirectStandardOutput = true;
                
                myprocess.StartInfo.FileName = getMsscmd;
                myprocess.StartInfo.Arguments = pars;
                myprocess.Start();

                string output = myprocess.StandardOutput.ReadToEnd();
                return output;
            }
            catch (Exception e0)
            {
               string error = "启动应用程序时出错！原因：" + e0.Message;
               return error;
            }
        }
        public static string Deploy()
        {
            return Execute(getDeployPars);
        }
        public static string ImportShape(string path,string name)
        {
            shapePath = path;
            importTbName = name;
            return Execute(getImportShapePars);
        }
    }
}
